package member.order.action;

import m.common.action.ActionRequest;
import m.common.action.Param;
import m.common.action.RequestMethod;
import m.common.model.Model;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import member.base.action.BaseAction;
import member.base.model.StoreInfo;
import member.base.model.UserInfo;
import member.base.util.PowerUtil;
import member.order.model.RechargeOffset;
import member.order.model.StoreSettle;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;


@ActionRequest(name = "action/order/storeSettle",description = "店铺结算")
public class StoreSettleAction extends BaseAction {
    @RequestMethod(description = "列表")
    public JSONMessage getList() throws Exception {
        ModelQueryList queryList=toQueryList(StoreSettle.class,"*","inputStore.name","inputStore.color","settleStore.name","settleStore.color");
        queryList.addCondition(QueryCondition.or(
            QueryCondition.eq("inputStore.oid",PowerUtil.getStore().getOid()),
            QueryCondition.eq("settleStore.oid",PowerUtil.getStore().getOid())
        ));
        return JSONMessage.success("查询成功").push("page", ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "创建")
    public JSONMessage create(@Param("settleStore") StoreInfo settleStore) throws Exception {
        if(ObjectUtil.isEmptyOid(settleStore)) throw new MException("请选择结算店铺");
        if(PowerUtil.getStore().getOid().equals(settleStore.getOid())) throw new MException("不能和自己结算");
        StoreSettle model=new StoreSettle();
        model.setSettleStore(settleStore);
        model.setOid(GenerateID.generatePrimaryKey());
        model.setInputStore(PowerUtil.getStore());
        model.setInputUser(PowerUtil.getUser());
        model.setInputDate(new Date());
        model.setStatus(StoreSettle.StatusSave);
        ModelUpdateUtil.insertModel(model);
        return JSONMessage.success("创建成功").push("oid",model.getOid());
    }
    @RequestMethod(description = "获取信息")
    public JSONMessage getInfo(@Param("oid") String oid) throws Exception {
        StoreSettle model;
        if(StringUtil.isSpace(oid)){
            throw new MException("结算信息不存在");
        }else{
            ModelQueryList queryList=ModelQueryList.instance(StoreSettle.class,
                new String[]{"*","inputStore.name","inputStore.color","settleStore.name","settleStore.color",
                    "inputStore.tel","inputStore.address","settleStore.tel","settleStore.address",
                    "inputUser.realName","inputUser.phoneNum","confirmUser.realName","confirmUser.phoneNum"},
                null,QueryCondition.eq("oid",oid)
            );
            model=queryList.getModel();
        }
        PowerUtil.verifyBusiness(model.getInputStore().getBusiness());
        return JSONMessage.success("查询成功")
            .push("model",model)
            .push("editable", StoreSettle.StatusSave.equals(model.getStatus())
                &&PowerUtil.hasPower(model.getInputUser()))
            .push("confirmable",StoreSettle.StatusConfirm.equals(model.getStatus())
                &&PowerUtil.hasPower(UserInfo.PowerStore)
                &&PowerUtil.hasPower(model.getSettleStore()));
    }
    @RequestMethod(description = "删除",transaction = true)
    public JSONMessage delete(@Param("model") StoreSettle model) throws MException, SQLException {
        List<RechargeOffset> ls=ModelQueryList.getModelList(RechargeOffset.class,new String[]{"*"},null,
                QueryCondition.eq("settle.oid",model.getOid()));
        ls.forEach(item->item.setSettle(null));
        ModelUpdateUtil.updateModels(ls.toArray(new RechargeOffset[0]),"settle.oid");
        ModelUpdateUtil.deleteModel(model);
        return JSONMessage.success("删除成功");
    }
    @RequestMethod(description = "提交",transaction = true)
    public JSONMessage submit(@Param("model") StoreSettle model) throws SQLException, MException {
        List<RechargeOffset> ls=ModelQueryList.getModelList(RechargeOffset.class,new String[]{"*"},null,
                QueryCondition.eq("settle.oid",model.getOid()));
        if(ls.isEmpty()) throw new MException("未添加结算明细");
        model.setStatus(StoreSettle.StatusConfirm);
        ModelUpdateUtil.updateModel(model,"remark","status");
        return JSONMessage.success("提交成功");
    }
    @RequestMethod(description = "退回",transaction = true)
    public JSONMessage back(@Param("model") StoreSettle model) throws MException {
        model.setStatus(StoreSettle.StatusSave);
        ModelUpdateUtil.updateModel(model,"confirmRemark","status");
        return JSONMessage.success("退回成功");
    }
    @RequestMethod(description = "确认",transaction = true)
    public JSONMessage confirm(@Param("model") StoreSettle model) throws SQLException, MException {
        List<RechargeOffset> ls=ModelQueryList.getModelList(RechargeOffset.class,new String[]{"*"},null,
                QueryCondition.eq("settle.oid",model.getOid()));
        ls.forEach(item->item.setStatus(StoreSettle.StatusDone));
        model.setStatus(StoreSettle.StatusDone);
        ModelUpdateUtil.updateModel(model,"confirmRemark","status");
        ModelUpdateUtil.updateModels(ls.toArray(new RechargeOffset[0]),"status");
        return JSONMessage.success("确认成功");
    }
}
